package com.njupt.Tree;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/25 9:42
 * @Description: 226. 翻转二叉树
 * @Version: 1.0
 */


public class InvertTree {

    public TreeNode invertTree(TreeNode root) {
        reverse(root);
        return root;
    }

    private void reverse(TreeNode root) {
        if(root == null) return;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        reverse(root.left);
        reverse(root.right);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);

        InvertTree test = new InvertTree();
        TreeNode treeNode = test.invertTree(root);

        System.out.println();

    }
}
